package com.supersit.greensproject.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.supersit.greensproject.R;
import com.supersit.greensproject.entity.BaseBase;

import java.util.ArrayList;

/**
 * Created by cong on 2016/7/22.
 */
public class BaseDataListAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<BaseBase> data;

    public BaseDataListAdapter(Context context, ArrayList<BaseBase> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {

        return data == null ? 0 : data.size();
    }

    @Override
    public Object getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        HolderView holderView;
        if (convertView == null) {
            holderView = new HolderView();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_base_data_list, null);
            holderView.baseName = (TextView) convertView.findViewById(R.id.tv_base_name);
            holderView.baseArea = (TextView) convertView.findViewById(R.id.tv_base_area);
            holderView.baseProportion = (TextView) convertView.findViewById(R.id.tv_base_proportion);
            holderView.isDelete = (ImageView) convertView.findViewById(R.id.iv_is_delete);
            convertView.setTag(holderView);
        } else {
            holderView = (HolderView) convertView.getTag();
        }
        holderView.baseName.setText(data.get(position).getBaseName());
        holderView.baseArea.setText((context.getResources().getStringArray(R.array.area))[data.get(position).getAreaId()]);
        holderView.baseProportion.setText(data.get(position).getProportion());
        if (data.get(position).getOperationsTatus() == 0) {
            holderView.isDelete.setVisibility(View.VISIBLE);
        } else {
            holderView.isDelete.setVisibility(View.GONE);
        }
        return convertView;
    }

    class HolderView {
        TextView baseName;
        TextView baseArea;
        TextView baseProportion;
        ImageView isDelete;
    }

    public void setData(ArrayList<BaseBase> data) {
        this.data = data;
    }
}
